package com.xsp.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsp.eduservice.entity.EduTeacher;
import com.xsp.eduservice.entity.vo.TeacherQuery;
import com.xsp.eduservice.mapper.EduTeacherMapper;
import com.xsp.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-15
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    //条件分页
    @Override
    public Page<EduTeacher> pageCourseCondition(long page, long limit, TeacherQuery teacherQuery) {
        //创建分页对象
        Page<EduTeacher> teacherPage = new Page<>(page,limit);
        //创建Wrapper条件对象
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        //保存TeacherQuery对象各个属性的值
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        //对传入的TeacherQuery各个属性值进行判断，如果不是空则进入wrapper添加条件
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }

        if(!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_modified",end);
        }

        //根据创建时间降序排列
        queryWrapper.orderByDesc("gmt_create");

        //将分页对象和条件对象传入
        baseMapper.selectPage(teacherPage,queryWrapper);

        return teacherPage;
    }

    @Override
    public Map<String, Object> pageTeacherList(Page<EduTeacher> teacherPage) {
        //把分页数据封装到teacherPage对象
        baseMapper.selectPage(teacherPage, null);
        //拿到数据
        List<EduTeacher> records = teacherPage.getRecords();
        long current = teacherPage.getCurrent();
        long pages = teacherPage.getPages();
        long size = teacherPage.getSize();
        long total = teacherPage.getTotal();
        boolean hasNext = teacherPage.hasNext();
        boolean hasPrevious = teacherPage.hasPrevious();
        //放入数据
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);//存放了分页的数据对象
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        //返回数据
        return map;
    }
}
